﻿#region Using directives

using System;
using System.Xml;
using System.ComponentModel;
using System.Collections.Specialized;
using MCMLXVII.Client.Common.Session;
using MCMLXVII.BackEnd.Services.SystemConfiguration.Messages;
using MCMLXVII.BackEnd.Services.Common.Messages;
using MCMLXVII.BackEnd.Services.OrganizationChart.Messages;
using MCMLXVII.BackEnd.Services.OrganizationChart.Interfaces;
using MCMLXVII.Tools.OrganizationChart.SmartEditors;
using MCMLXVII.Core.Common;
using System.Collections.Generic;

#endregion

namespace MCMLXVII.Tools.OrganizationChart.MembersClasses
{
    public abstract class MemberBase: iOrganizationTreeMemberClass 
    {
        
        public MemberBase()
        {

        }

        [Browsable(false)]
        public void MarkChanges()
        {
            _Haschanges = true;
        }
        
        protected int _MemberID;
        protected string _Description;
        protected XmlDocument _ClassProperties;
        protected XmlDocument _BaseClassProperties;
        protected int _NodeID;
        protected int _MemberClass;
        protected bool _Haschanges;
        protected string _ClassDescription;
        protected int _UpdateSequence;

        private static string[] _ContentLanguagesList;
        private static msgLanguajesListResponse _LanguageList=null;
        private static string _DefaultLanguage = null;
        private static string[] _DirectoryServicesNames = null;
        private static int[] _DirectoryServicesIDs = null;
          
        
        #region iOrganizationTreeMemberClass Members

        [Browsable(false)]
        public virtual ucSmartBaseEditor GetEditor(bool UseGridEditor)
        {
            return null;
        }

        [Browsable(false)]
        public int MemberClass
        {
            get { return _MemberClass; }
        }

        [Browsable(false)]
        public virtual string MemberClassName
        {
            get { return "Base"; }
        }

        [Browsable(false)]
        public int UpdateSequence
        {
            get { return _UpdateSequence; }
            set { _UpdateSequence = value; }
        }



        [Browsable(false)]
        public abstract System.Drawing.Icon GetIcon();
        

        public void InitClass(msgOrganizationTreeMembersClass classInfo)
        {
            _BaseClassProperties = classInfo.BasicProperties.Document;
            _MemberClass = classInfo.OrganizationMemberClassID;
            _ClassDescription = classInfo.Description;
        }

        public virtual void LoadMemberData(msgOrganizationMemberInfo memberInfo)
        {
            _MemberID = memberInfo.MemberID;
            _Description = memberInfo.Description;
            _ClassProperties = memberInfo.ClassProperties.Document;
            _NodeID = memberInfo.NodeID;
            _UpdateSequence = memberInfo.UpdateSequence;
            _Haschanges = false;
        }

        public abstract msgOrganizationMemberInfo GetMemberData();

        public bool HasChanges()
        {
            return _Haschanges;
        }


        [Browsable(false)]
        public XmlDocument MemeberClassProperties
        {
            get
            {
                return _ClassProperties;
            }

            set
            {
                _ClassProperties = value;
                _Haschanges = true;
            }
        }

        [LocCategory("Identification"), ReadOnly(true), LocDescription("Identification_Description"), LocDisplayName("Identification_DisplayName")]
        public int MemberID
        {
            get
            {
                return _MemberID;
            }
        }

        [LocCategory("Identification"), LocDescription("Name_Description"), LocDisplayName("Name_DisplayName")]
        public string Description
        {
            get
            {
                return _Description;
            }

            set
            {
                _Description = value;
                _Haschanges = true;
            }
        }

        [LocCategory("Identification"), ReadOnly(true), LocDescription("MemberClass_Description"), LocDisplayName("MemberClass_DisplayName")]
        public string MemberClassDescription
        {
            get { return _ClassDescription; }
        }


        #endregion

        public static msgMembershipInfo FindMemberInfo(int MemberID)
        {
            return BusinessActions.GetOrganizationTreeMemberDesc(MemberID);
        }

        public static msgMembershipInfo[] FindMembershipInfo(int ClassID, string FindPattern)
        {
            return BusinessActions.GetOrganizationTreeMembers(ClassID, FindPattern);
        }
        public static msgMembershipInfo[] FindRoleMembershipInfo(int ClassID, string FindPattern)
        {
            return BusinessActions.GetOrganizationTreeRoleMembers(ClassID, FindPattern);
        }

        public static Dictionary<string,string> LanguageList()
        {
            try
            {
            if (_LanguageList == null)
                _LanguageList = BusinessActions.GetContentLanguagesList(true);
            return _LanguageList.GetLanguages();
            }
            catch
            {
                Dictionary<string,string> tmp = new Dictionary<string,string>();
                tmp.Add("EN-US", "English");
                return tmp;
            }
        }

        public static string DefaultLanguage()
        {
            try
            {
                if (_DefaultLanguage == null)
                    _DefaultLanguage = BusinessActions.GetDefaultLanguage();
                return _DefaultLanguage;
            }
            catch
            {
                return "EN-US";
            }
        }
        
        private static IAsyncResult _DefaultLangAsync;

        public static void InitDefualtLanguage()
        {
                if (_DefaultLanguage == null)
                {
                 //   try
                 //   {
                 //       _DefaultLangAsync = ActionDispatcher.BeginExecuteAsync<msgVoidRequest, string>("GetDefaultLanguage", new msgVoidRequest(), new AsyncCallback(MemberBase.InitDefaultlanguageCallBack));
                 //   }
                 //   catch
                 //   {
                        _DefaultLanguage = BusinessActions.GetDefaultLanguage();
                 //   }
                }
        }

        public static string[] ContentsLanguageList()
        {

            if (_ContentLanguagesList == null)
                _ContentLanguagesList = BusinessActions.GetContentLanguagesList(false).GetLanguageKeyLists();
            return _ContentLanguagesList;
        }

        public static string[] DirectoryServicesNames()
        {
            if (_DirectoryServicesNames == null)
                            LoadDirectoryServices();

            return _DirectoryServicesNames;
        }

        public static int[] DirectoryServicesIDs()
        {
            if (_DirectoryServicesIDs == null)
                LoadDirectoryServices();

            return _DirectoryServicesIDs;
        }

        private static void LoadDirectoryServices()
        {
            int Length = StaticCache.SecurityProviders.Length;
            
            _DirectoryServicesNames = new string[Length];
            _DirectoryServicesIDs = new int[Length];
            int i = 0;
            foreach (msgSecurityProvider sp in StaticCache.SecurityProviders)
            {
                _DirectoryServicesNames[i] = sp.Description;
                _DirectoryServicesIDs[i] = sp.SecProviderID;
                i++;
            }
        }


    }
}
